package com.qupai.controller.admin;


import com.qupai.dto.PtPrizesDTO;
import com.qupai.dto.PtPrizesPageQueryDTO;
import com.qupai.entity.PtPrize;
import com.qupai.result.PageResult;
import com.qupai.result.Result;
import com.qupai.service.PtPrizesService;
import com.qupai.vo.PtPrizesVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController("adminPtPrizesController")
@RequestMapping("/admin/ptprizes")
@Tag(name = "普通-奖品管理相关接口")
@Slf4j
@RequiredArgsConstructor
public class PtPrizeController {

    private final PtPrizesService prizesService;
    private final RedisTemplate<String, Object> redisTemplate;


    @PostMapping
    @Operation(summary = "新增奖品")
    public Result<String> save(@RequestBody PtPrizesDTO prizesDTO) {
        log.info("新增奖品:{}", prizesDTO);
        // 调用service保存奖品信息
        prizesService.save(prizesDTO);

        //清理缓存数据
        String key = "ptprizes_" + prizesDTO.getCollegesId();
        cleanCache(key);

        return Result.success();
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询奖品")
    public Result<PageResult> page(@ParameterObject PtPrizesPageQueryDTO prizesPageQueryDTO){
        log.info("分页查询奖品:{}", prizesPageQueryDTO);
        PageResult pageResult=prizesService.pageQuery(prizesPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 奖品的批量删除
     */

    @DeleteMapping
    @Operation(summary = "批量删除奖品")
    public Result<String> delete(@RequestParam List<Long> ids){
        log.info("批量删除奖品:{}",ids);
        prizesService.deleteBatch(ids);

        //将所有的奖品数据缓存数据清理掉，所有以prizes_开头的key
        cleanCache("ptprizes_*");
        return Result.success();
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据id查询奖品")
    public Result<PtPrizesVO>getById (@PathVariable Long id){
        log.info("根据id查询奖品:{}",id);
        PtPrizesVO prizesVO=prizesService.getById(id);
        return Result.success(prizesVO);
    }

    @PutMapping
    @Operation(summary = "修改奖品")
    public Result<String> update(@RequestBody PtPrizesDTO prizesDTO){
        log.info("修改奖品:{}",prizesDTO);
        prizesService.update(prizesDTO);

        //将所有的奖品数据缓存数据清理掉，所有以prizes_开头的key
        cleanCache("ptprizes_*");
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @Operation(summary = "启用/禁用奖品状态")
    public Result<String> startOrStop(@PathVariable Integer status, Long id){
        log.info("启用/禁用奖品状态：{}，{}",status,id);
        //调用service
        prizesService.startOrStop(status,id);

        //将所有的奖品数据缓存数据清理掉，所有以prizes_开头的key
        cleanCache("ptprizes_*");
        return Result.success();
    }

    /**
     * 根据各分类id查询奖品
     */
    @GetMapping("/list")
    @Operation(summary = "根据各分类id查询奖品")
    public Result<List<PtPrizesVO>> list(PtPrize prizes){
        List<PtPrizesVO> list = prizesService.selectPrizesList(prizes);
        log.info("根据各分类id查询奖品：{}",list);
        return Result.success(list);
    }

    private void cleanCache(String pattern){
        Set<String> keys = redisTemplate.keys(pattern);
        if (keys != null && !keys.isEmpty()) {
            redisTemplate.delete(keys);
            log.info("清除缓存成功：{}", keys);
        }else{
            log.info("清除缓存失败：{}", keys);
        }
    }

}
